<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
date_default_timezone_set('PRC'); //设置中国时区 
/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
class Create extends CI_Controller{
    public function index(){
        $homeArr = $this->Data_model->get_data(array(),'','',-1,-1,'home');
        $res = array('homeArr' => $homeArr);
        $this->load->view("create",$res);
    }
    function dian(){
        $dian         = trim($_POST['dian']);
        $id           = trim($_POST['id']);
        $month        = trim($_POST['datetime']);
        $lastmonth    = date("Y-m",  strtotime("-1 month",  strtotime($month)));
        $now = date('Y-m-d h:i:s',time());
        
        $return = $this->Data_model->get_single_data(array("homeid" => $id,"yuedu" => $lastmonth),"shuidian");
        if(empty($return)){
            $shui     = "0";
            $kongtiao = "0"; 
        }else{
            $shui     = $return["shui"];
            $kongtiao = $return["kongtiao"];
        }
        $res = $this->Data_model->get_data_num(array("homeid" => $id,"yuedu" => $month),'',"shuidian");
        if($res == 0){
            $this->Data_model->add_data(array("homeid" => $id,"dian" => $dian,"shui" => $shui,"kongtiao" => $kongtiao,"yuedu" => $month,"createtime" => $now),"shuidian");
        }elseif($res > 0){
            $this->Data_model->update_data(array("homeid" => $id,"yuedu" => $month),array("dian" => $dian,"lastupdate" => $now),"shuidian");
        }
        $json_date = array(
            "month"      => $month,
            "dian"       => $dian,
            "shui"       => $shui,
            "kongtiao"   => $kongtiao ,
            "lastmonth"  => $lastmonth,
            "res"        => $res
        );
        echo json_encode($json_date);
    }
    function shui(){
        $shui         = trim($_POST['shui']);
        $id           = trim($_POST['id']);
        $month        = trim($_POST['datetime']);
        $lastmonth    = date("Y-m",  strtotime("-1 month"));
        $now = date('Y-m-d h:i:s',time());
        if($shui == 0){
            $return = $this->Data_model->get_single_data(array("homeid" => $id,"yuedu" => $lastmonth),"shuidian");
            $shui = $return["shui"];
            
        }
        $res = $this->Data_model->get_data_num(array("homeid" => $id,"yuedu" => $month),'',"shuidian");
        if($res == 0){
            $this->Data_model->add_data(array("homeid" => $id,"shui" => $shui,"yuedu" => $month,"createtime" => $now),"shuidian");
        }elseif($res > 0){
            $this->Data_model->update_data(array("homeid" => $id,"yuedu" => $month),array("shui" => $shui,"lastupdate" => $now),"shuidian");
        }
        $json_date = array(
            "shui"       => $shui,
            "lastmonth"  => $lastmonth,
            "res"        => $res,
            "return"     => $return
        );
        echo json_encode($json_date);
    }
    function kongtiao(){
        $kongtiao         = trim($_POST['kongtiao']);
        $id           = trim($_POST['id']);
        $month        = trim($_POST['datetime']);
        $lastmonth    = date("Y-m",  strtotime("-1 month"));
        $now = date('Y-m-d h:i:s',time());
        
        if($kongtiao == 0){
            $return = $this->Data_model->get_single_data(array("homeid" => $id,"yuedu" => $lastmonth),"shuidian");
            var_dump($return);
            $kongtiao = $return["kongtiao"];
        }
        $res = $this->Data_model->get_data_num(array("homeid" => $id,"yuedu" => $month),'',"shuidian");
        if($res == 0){
            $this->Data_model->add_data(array("homeid" => $id,
                                                "kongtiao" => $kongtiao,
                                                "yuedu" => $month,
                                                "createtime" => $now),"shuidian");
        }elseif($res > 0){
            $this->Data_model->update_data(array("homeid" => $id,
                                                    "yuedu" => $month),array("kongtiao" => $kongtiao,"lastupdate" => $now),"shuidian");
        }
        $json_date = array(
            "kongtiao"       => $kongtiao,
            "lastmonth"  => $lastmonth,
            "res"        => $res
        );
        echo json_encode($json_date);
    }
}

